<template>
    <div id="parts">
        <el-button @click="insertList" class="slot-btn-top" type="primary" size="small">增加服务</el-button>
        <el-table :data="listdata.slice((currentPage-1)*pagesize,currentPage*pagesize)" stripe border :height="tableHeight" :header-cell-style="getRowClass">
            <el-table-column fixed type="index" label="*" sortable min-width="40"></el-table-column>
            <el-table-column prop="serviceName" label="服务名称" sortable  min-width="200"></el-table-column>
            <el-table-column prop="serviceDesc" label="服务描述" sortable  min-width="1000"></el-table-column>
            <el-table-column fixed="right" label="操作" sortable align="center" min-width="80">
                <template slot-scope="scope">
                    <el-button @click="updateList(scope.row)" type="text" size="small">编辑</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage" :page-sizes="[5,10, 15,20, 25,30,50, 100]" :page-size="pagesize" layout="total, sizes, prev, pager, next, jumper" :total="listdata.length"></el-pagination>

        <el-dialog :title="dialogTitle" width="400px" center :visible.sync="isDialog" :close-on-click-modal="false">
            <el-form :inline="true" class="demo-form-inline" v-model="insertData" label-position="right" label-width="100px">
                <el-form-item label="服务名称"><el-input v-model="insertData.serviceName"></el-input></el-form-item>
                <el-form-item label="服务描述"><el-input type="textarea" :rows="10" v-model="insertData.serviceDesc" style="width:200px"></el-input></el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer" style="text-align: center;"><el-button @click="submitList" type="primary">确 定</el-button></div>
        </el-dialog>
    </div>
</template>

<script>
    export default {
        data() {
            let token = sessionStorage.getItem("tokenId");
            return {
                tokenId: token,
                currentPage: 1,
                pagesize: 10,
                tableHeight: document.documentElement.clientHeight - 160,

                listdata:[],
                isDialog:false,
                dialogTitle:"",
                hospitalName:[],
                applyOffice:[],
                product:[],
                insertData:{
                    serviceName:"",
                    serviceDesc:""

                }
            };
        },
        created() {
            this.selectAll();
        },
        methods: {
            //查询服务类型列表
            selectAll() {
                let _this = this;
                this.$http({
                    url:
                        "/api/adscm/serviceProvider/jyServiceType/selectAll?tokenId=" +
                        _this.tokenId,
                    method: "post",
                    headers: { "X-Requested-With": "XMLHttpRequest" }
                }).then(function(res) {
                    if (res.data.statusCode == 200) {
                        _this.listdata = res.data.content;
                    } else {
                        _this.$message.error(res.data.desc);
                    }
                }).catch(function(catchres) {
                        _this.$message.error("加载失败！请重新加载...");
                    });
            },

            insertList() {
                this.isDialog = true;
                this.dialogTitle = "厂商增加服务";
                this.insertData = {
                    serviceName:"",
                    serviceDesc:""
                }
            },
            updateList(row){
                this.isDialog = true;
                this.dialogTitle = "编辑服务";
                this.insertData =row
            },
            submitList() {
                let _this = this;
                let url;
                if (this.dialogTitle === "厂商增加服务") {
                    url =
                        "/api/adscm/serviceProvider/jyServiceType/insert?tokenId=" +
                        _this.tokenId;
                }else if (this.dialogTitle === "编辑服务") {
                    url = "/api/adscm/serviceProvider/jyServiceType/update?tokenId=" + _this.tokenId;
                }
                this.$http({
                    url: url,
                    method: "post",
                    headers: { "X-Requested-With": "XMLHttpRequest" },
                    data: _this.insertData
                }).then(function(res) {
                        if (res.data.statusCode == 200) {
                            _this.isDialog = false;
                            _this.selectAll()
                        }
                    }).catch(function(catchres) {
                        _this.$message.error("加载失败！请重新加载...");
                    });
            },

            handleSizeChange: function (size) {
                this.pagesize = size;
            },
            handleCurrentChange: function (currentPage) {
                this.currentPage = currentPage;
            },

            timestampToTime(timestamp) {
                if (timestamp) {
                    let date = new Date(timestamp);
                    let Y = date.getFullYear() + "-";
                    let M =
                        (date.getMonth() + 1 < 10
                            ? "0" + (date.getMonth() + 1)
                            : date.getMonth() + 1) + "-";
                    let D =
                        (date.getDate() < 10 ? "0" + date.getDate() : date.getDate()) + " ";
                    let h =
                        (date.getHours() < 10 ? "0" + date.getHours() : date.getHours()) +
                        ":";
                    let m =
                        (date.getMinutes() < 10
                            ? "0" + date.getMinutes()
                            : date.getMinutes()) + ":";
                    return timestamp ? Y + M + D + h + m + "00" : "";
                } else {
                    return "";
                }
            }
        }


    };
</script>

<style  scoped>

    #parts .el-table--small {
        font-size: 13px;
    }

    #parts .el-table .cell,
    #parts .el-table th div {
        padding-left: 0;
        padding-right: 0;
    }

    #parts .el-tab-pane.dialog1 .el-form {
        margin-bottom: 30px;
    }

</style>
